/**
 *====================================================
 * 文件名称: DeliveryServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.DeliveryEntity;
import com.yuexunit.finance.project.entity.lexicon.DeliveryStateEnum;
import com.yuexunit.finance.project.entity.lexicon.SupervisionStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: DeliveryServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(DeliveryService.class)
public class DeliveryServiceImpl implements DeliveryService {

	@Override
	public Long createDelivery(DeliveryEntity entity) {
		entity.setDeliveryState(DeliveryStateEnum.WAIT_CONFIRM);
		entity.setDeliveryNumber(SettingConstants.PRE_NUMBER_DELIVERY + RandomUtil.generateTimeNumber());
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getDeliveryId();
	}

	@Override
	public void updateDelivery(DeliveryEntity entity) {
		DeliveryEntity persist = inqureDelivery(entity.getDeliveryId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateDelivery(DeliveryEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public DeliveryEntity inqureDelivery(Long deliveryId) {
		return PersistUtil.get(DeliveryEntity.class, deliveryId);
	}

	@Override
	public PageList<DeliveryEntity> inqureDeliveryPageList(Long[] deliveryIds, Long financeId, String financeNumber, Long deliveryNoticeId, Long logisticsCompanyId, Long clerkId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DeliveryEntity.class);
		if (deliveryIds != null) {
			criteria.add(Restrictions.in("deliveryId", deliveryIds));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (deliveryNoticeId != null) {
			criteria.add(Restrictions.eq("deliveryNoticeId", deliveryNoticeId));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (clerkId != null) {
			criteria.add(Restrictions.eq("clerkId", clerkId));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countRelieveDelivery() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryEntity.class);
		criteria.add(Restrictions.eq("signState", Boolean.TRUE));
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		criteria.add(Restrictions.eq("supervisionState", SupervisionStateEnum.ALREADY_SUPERVISE));
		criteria.addOrder(Order.desc("createDateTime"));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("inventoryId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<DeliveryEntity> inqureDeliveryInProcess(Long logisticsCompanyId, Set<DeliveryStateEnum> deliveryStates) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DeliveryEntity.class);
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		
		if (deliveryStates.size() > 0) {
			criteria.add(Restrictions.in("deliveryState", deliveryStates));
		}
		return PersistUtil.get(criteria);
	}

}
